package com.bjsxt.config.feign;


import com.google.common.net.HttpHeaders;
import feign.RequestInterceptor;
import feign.RequestTemplate;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Configuration
public class OAuth2FeiginConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //1.我们从request 上下文环境获取token
        RequestAttributes requestAttributes =  RequestContextHolder.getRequestAttributes();
        if(requestAttributes == null){
            log.info("没有请求的上下文，故无法进行token的传递");
        }

        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            String header = request.getHeader(HttpHeaders.AUTHORIZATION);

            if(!StringUtils.isEmpty(header)){
                requestTemplate.header(HttpHeaders.AUTHORIZATION,header);
                log.info("传递token成功，token的值为:{}",header);
            }

    }















}
